class Solution(object):
    def powerfulIntegers(self, x, y, bound):
        out = []
        if bound < 2:
            return out
        if x == 1:
            if y == 1:
                return [1]
            else:
                k = 0
                while x + y ** k <= bound:
                    out.append(x + y ** k)
                    k += 1
                return out
        else:
            if y == 1:
                k = 0
                while x ** k + y <= bound:
                    out.append(x ** k + y)
                    k += 1
                return out
            else:
                out = set()
                i = 0
                while True:
                    if x ** i + y ** 0 <= bound:
                        j = 0
                        while True:
                            if x ** i + y ** j <= bound:
                                out.add(x ** i + y ** j)
                                j += 1
                            else:
                                break
                        i += 1
                    else:
                        break
                return list(out)
